//
//  ByCellStoreIndex.m
//  chequanbao_ios
//
//  Created by pro on 17/5/25.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByCellStoreIndex.h"

@implementation ByCellStoreIndex
{
    UIView          *_bgView;
    UIImageView     *_iconView;
    UILabel         *_titleLabel;
    UILabel         *_detailLabel;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.backgroundColor = [UIColor clearColor];
        [self setupUI];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        [self addGestureRecognizer:tap];
    }
    return self;
}
- (void)setupUI {
    
    
    
    _bgView = [[UIView alloc] init];
    _bgView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.9];
    [self.contentView addSubview:_bgView];
    
    _iconView = [[UIImageView alloc] init];
    [_bgView addSubview:_iconView];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.adjustsFontSizeToFitWidth = YES;
    _titleLabel.font = FONT(15);
    _titleLabel.textColor = rgba(27, 161, 253, 1);
    _titleLabel.textAlignment = NSTextAlignmentLeft;
    [_bgView addSubview:_titleLabel];
    
    
    _detailLabel = [[UILabel alloc] init];
    _detailLabel.adjustsFontSizeToFitWidth = YES;
    _detailLabel.numberOfLines = 2;
    _detailLabel.font = FONT(15);
    _detailLabel.textColor = [UIColor blackColor];
    _detailLabel.textAlignment = NSTextAlignmentLeft;
    [_bgView addSubview:_detailLabel];

}

- (void)setDataDic:(NSDictionary *)dataDic {
    
    _dataDic = dataDic;
    
    [self setNeedsLayout];
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    CGFloat margin = 10;
    CGFloat x, y, width, height;
    
    x = 0;
    y = 0;
    width = SCREEN_WIDTH - 20;
    height = self.height;
    _bgView.layer.cornerRadius = 10;
    _bgView.frame = CGRectMake(x, y, width, height);
    
    x = margin;
    y = margin;
    height = self.height - y * 2;
    width = height / 3 * 4;
    _iconView.layer.cornerRadius = 4;
    _iconView.frame = CGRectMake(x, y, width, height);
    
    x = _iconView.right + margin;
    y = 0;
    width = _bgView.width - 10 - x;
    height = 20;
    _titleLabel.frame = CGRectMake(x, y, width, height);

    
    x = _titleLabel.left;
    y = 0;
    width = _bgView.width - 10 - x;
    height = 0;
    _detailLabel.frame = CGRectMake(x, y, width, height);
    
    
    /*
     "id": "2",
     "name": "测试商城商家2",
     "logo": "64",
     "synopsis": "232",
     "link": "23233",
     "other_info": [],
     "create_time": "0",
     "display_order": "0",
     "status": "0"
     */
    /*
     模拟数据
     */
    NSString *urlStr = BOYE_PICTURE_VIEW_URL(_dataDic[@"logo"]);
    NSString *title = _dataDic[@"name"];
    NSString *detail = _dataDic[@"synopsis"];
    
    CGFloat detailHeight = MIN([detail getSizeOfString:15].height, 35);
    CGFloat remain = (_bgView.height - _titleLabel.height - detailHeight - 5) / 2;
    
    [_iconView setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:DEFAULT_IMAGE completed:nil];
    
    _titleLabel.top = remain;
    _titleLabel.text = title;
    
    _detailLabel.height = detailHeight;
    _detailLabel.top = _titleLabel.bottom + 5;
    _detailLabel.text = detail;
}

- (void)tapAction {
    
    if (self.tapBlock) {
        self.tapBlock(_dataDic[@"link"], _dataDic[@"name"]);
    }
}

@end
